<?php
require_once('controller.php');
require_once('beer_model.php');
require_once('brewery_model.php');
require_once('../lib/Validate.php');


/**
 * beer_controller Class
 * Controller for the beer
 * 
 * @author Victor Nava
 *
 */
class beer_controller extends controller{
	public $beer;
	
	/**
	 * Constructor
	 * @return void
	 */
	function __construct(){
		//load the model
		$this->beer = new beer_model();
	}

	/**
	 * Default action to perform when no action is called
	 * @param $args page
	 * @return void
	 */
	function index($args=null){
		if(!isset($this->request['page'])){
			$this->request['page'] = 0;
		}
		$view->beers = $this->beer->getAll($this->request);
		$view->countries = $this->beer->getCountryList();
		$view->prevPageLink = $this->getPrevNextPage("prev");
		$view->nextPageLink = $this->getPrevNextPage("next");
		include('beer_index_view.php');
	}

	/**
	 * Same as view();
	 */
	function get(){
		$this->view();
	}

	/**
	 * View a beer action
	 * @return void
	 */
	function view(){
		$view->beers = array();
		if(isset($this->request['id'])){
			$id = $this->request['id'];
			$view->beer = $this->beer->get($id);
			$view->beer['rate_count'] = $this->beer->getRateCount($id);
			$view->offersLink =  "router.php?c=offer&a=find&beer_name=".urlencode($view->beer['name']);
		}
		include('beer_view.php');
	}

	/**
	 * Search for a beer action
	 * @param $args
	 * @return void
	 */
	function find($args=null){
		//TODO $args not needed
		
		if(!isset($this->request['page'])){
			$this->request['page'] = 0;
		}
		$view->beers = $this->beer->find($this->request);
		$view->countries = $this->beer->getCountryList();
		$view->request = $this->request;
		$view->prevPageLink = $this->getPrevNextPage("prev");
		$view->nextPageLink = $this->getPrevNextPage("next");
		include('beer_index_view.php');
	}

	/**
	 * Create a beer action
	 * @return unknown_type
	 */
	function create(){
		$this->requireLogin();
		if ($_SERVER['REQUEST_METHOD'] == 'POST'){
			if( Validate::string($this->request['name'], array('min_length' => 1))){
				$beer = $this->beer->create($this->request);
				if($beer){
					$this->setFlash("Your beer has been added.");
				}
				else{
					$this->setFlash("There was a problem adding that beer, please try again.");
				}
			}
			else{
				$this->setFlash( "Name of the beer can't be blank, please try again.");
			}
		}
		$brewery = new brewery_model();
		$view->breweries = $brewery->getAll();
		$view->beerTypes = $this->beer->getBeerTypeList();
		include('beer_create_view.php');
	}

	/**
	 * Rate a beer action
	 * @return unknown_type
	 */
	function rate(){
		$this->requireLogin();
		$this->request["user_id"] = $_SESSION['user']['id'];
		$this->request["beer_id"] = $this->request['id'];
		$rate = new model("beer_rates");
		if($rate->create($this->request)){
			$this->setFlash("Thanks for rating!");
		}
		else {
			$this->setFlash("Upps! there was a problem with your rate, please try again.");
		}
		redirect($_SERVER['HTTP_REFERER']);
	}
}
?>